package com.cxx.purchasecharge.component.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import com.cxx.purchasecharge.component.bean.AccountingBean;
import com.cxx.purchasecharge.component.bean.AccountingTypeBean;
import com.cxx.purchasecharge.component.bean.ContactBean;
import com.cxx.purchasecharge.component.bean.ContactTypeBean;
import com.cxx.purchasecharge.component.bean.CustomerBean;
import com.cxx.purchasecharge.component.bean.GoodsBean;
import com.cxx.purchasecharge.component.bean.GoodsDepositoryBean;
import com.cxx.purchasecharge.component.bean.GoodsStorageBean;
import com.cxx.purchasecharge.component.bean.GoodsStorageCourseBean;
import com.cxx.purchasecharge.component.bean.GoodsTypeBean;
import com.cxx.purchasecharge.component.bean.GoodsUnitBean;
import com.cxx.purchasecharge.component.bean.LogSearchRequestForm;
import com.cxx.purchasecharge.component.bean.OrderBean;
import com.cxx.purchasecharge.component.bean.OrderItemBean;
import com.cxx.purchasecharge.component.bean.PaymentAccountBean;
import com.cxx.purchasecharge.component.bean.PaymentBean;
import com.cxx.purchasecharge.component.bean.SearchRequestForm;
import com.cxx.purchasecharge.component.bean.usersecurity.AuthorityBean;
import com.cxx.purchasecharge.component.bean.usersecurity.RoleBean;
import com.cxx.purchasecharge.component.bean.usersecurity.UserBean;
import com.cxx.purchasecharge.core.model.AccountingModeCode;
import com.cxx.purchasecharge.core.model.AccountingSearchForm;
import com.cxx.purchasecharge.core.model.GoodsStorageCourse;
import com.cxx.purchasecharge.core.model.LogSearchForm;
import com.cxx.purchasecharge.core.model.OrderSearchForm;
import com.cxx.purchasecharge.core.model.OrderStatusCode;
import com.cxx.purchasecharge.core.model.PaymentSearchForm;
import com.cxx.purchasecharge.core.model.persistence.Accounting;
import com.cxx.purchasecharge.core.model.persistence.AccountingType;
import com.cxx.purchasecharge.core.model.persistence.Authority;
import com.cxx.purchasecharge.core.model.persistence.Contact;
import com.cxx.purchasecharge.core.model.persistence.ContactType;
import com.cxx.purchasecharge.core.model.persistence.Customer;
import com.cxx.purchasecharge.core.model.persistence.Goods;
import com.cxx.purchasecharge.core.model.persistence.GoodsDepository;
import com.cxx.purchasecharge.core.model.persistence.GoodsStorage;
import com.cxx.purchasecharge.core.model.persistence.GoodsType;
import com.cxx.purchasecharge.core.model.persistence.GoodsUnit;
import com.cxx.purchasecharge.core.model.persistence.Order;
import com.cxx.purchasecharge.core.model.persistence.OrderItem;
import com.cxx.purchasecharge.core.model.persistence.Payment;
import com.cxx.purchasecharge.core.model.persistence.PaymentAccount;
import com.cxx.purchasecharge.core.model.persistence.Role;
import com.cxx.purchasecharge.core.model.persistence.User;
import com.cxx.purchasecharge.core.util.DateUtils;
import com.cxx.purchasecharge.dal.DaoContext;

public class BeanConvertUtils
{
    public static UserBean user2UserBean (User user)
    {
        if (null != user)
        {
            UserBean userBean = new UserBean ();
            BeanUtils.copyProperties (user, userBean, new String[]
            { "id", "qq" });
            userBean.setId (user.getId () + "");
            userBean.setQq (user.getQq () + "");
            userBean.setBod (DateUtils.date2String (user.getBirthday (), DateUtils.DATE_PATTERN));
            userBean.setPhone (user.getMobilePhone ());
            userBean.setRoleBeans (roleList2RoleBeanList (user.getRoles ()));
            return userBean;
        }
        return null;
    }

    public static List <UserBean> userList2UserBeanList (List <User> userList)
    {
        if (null != userList)
        {
            List <UserBean> userBeanList = new ArrayList <UserBean> ();
            for (User user : userList)
            {
                userBeanList.add (user2UserBean (user));
            }
            return userBeanList;
        }
        return null;
    }

    public static User userBean2User (UserBean userBean)
    {
        if (null != userBean)
        {
            User user = new User ();
            BeanUtils.copyProperties (userBean, user, new String[]
            { "id", "qq" });
            if (StringUtils.isNotBlank (userBean.getId ()))
            {
                user.setId (Long.parseLong (userBean.getId ()));
            }
            if (StringUtils.isNotBlank (userBean.getQq ()))
            {
                user.setQq (Long.parseLong (userBean.getQq ()));
            }
            if (StringUtils.isNotBlank (userBean.getBod ()))
            {
                user.setBirthday (DateUtils.string2Date (userBean.getBod (), DateUtils.DATE_PATTERN));
            }
            if (StringUtils.isNotBlank (userBean.getPhone ()))
            {
                user.setMobilePhone (userBean.getPhone ());
            }
            user.setRoles (roleBeanList2RoleList (userBean.getRoleBeans ()));
            return user;
        }
        return null;
    }

    public static RoleBean role2RoleBean (Role role)
    {
        if (null != role)
        {
            RoleBean roleBean = new RoleBean ();
            BeanUtils.copyProperties (role, roleBean, new String[]
            { "id" });
            roleBean.setId (role.getId () + "");
            roleBean.setUserBeans (userList2UserBeanList (role.getUsers ()));
            roleBean.setAuthorityBeans (authorityList2AuthorityBeanList (role.getAuthorities ()));
            return roleBean;
        }
        return null;
    }

    public static Role roleBean2Role (RoleBean roleBean)
    {
        if (null != roleBean)
        {
            Role role = new Role ();
            BeanUtils.copyProperties (roleBean, role, new String[]
            { "id" });
            role.setId (StringUtils.isNotBlank (roleBean.getId ()) ? Long.parseLong (roleBean.getId ()) : 0);
            role.setAuthorities (authorityBeanList2AuthorityList (roleBean.getAuthorityBeans ()));
            return role;
        }
        return null;
    }

    public static List <Role> roleBeanList2RoleList (List <RoleBean> roleBeans)
    {
        List <Role> roles = new ArrayList <Role> ();
        if (!CollectionUtils.isEmpty (roleBeans))
        {
            for (RoleBean roleBean : roleBeans)
            {
                roles.add (roleBean2Role (roleBean));
            }
        }
        return roles;
    }

    public static List <RoleBean> roleList2RoleBeanList (List <Role> roles)
    {
        List <RoleBean> roleBeans = new ArrayList <RoleBean> ();
        if (!CollectionUtils.isEmpty (roles))
        {
            for (Role role : roles)
            {
                roleBeans.add (role2RoleBean (role));
            }
        }
        return roleBeans;
    }

    public static AuthorityBean authority2AuthorityBean (Authority authority)
    {
        if (null != authority)
        {
            AuthorityBean authorityBean = new AuthorityBean ();
            BeanUtils.copyProperties (authority, authorityBean, new String[]
            { "id" });
            authorityBean.setId (authority.getId () + "");
            authorityBean.setRoleBeans (roleList2RoleBeanList (authority.getRoles ()));
            //authorityBean.setResourceBeans (resourceList2ResourceBeanList (authority.getResources ()));
            return authorityBean;
        }
        return null;
    }

    public static List <AuthorityBean> authorityList2AuthorityBeanList (List <Authority> authorities)
    {
        List <AuthorityBean> authorityBeans = new ArrayList <AuthorityBean> ();
        if (!CollectionUtils.isEmpty (authorities))
        {
            for (Authority authority : authorities)
            {
                authorityBeans.add (authority2AuthorityBean (authority));
            }
        }
        return authorityBeans;
    }

    public static Authority authorityBean2Authority (AuthorityBean authorityBean)
    {
        if (null != authorityBean)
        {
            Authority authority = new Authority ();
            BeanUtils.copyProperties (authorityBean, authority, new String[]
            { "id" });
            authority.setId (StringUtils.isNotBlank (authorityBean.getId ()) ? Long.parseLong (authorityBean.getId ())
                                                                            : 0);
            //authority.setResources (resourceBeansList2ResourceList (authorityBean.getResourceBeans ()));
            return authority;
        }
        return null;
    }

    public static List <Authority> authorityBeanList2AuthorityList (List <AuthorityBean> authorityBeans)
    {
        List <Authority> authorities = new ArrayList <Authority> ();
        if (!CollectionUtils.isEmpty (authorityBeans))
        {
            for (AuthorityBean ab : authorityBeans)
            {
                authorities.add (authorityBean2Authority (ab));
            }
        }
        return authorities;
    }

    public static Goods goodsBean2Goods (GoodsBean goodsBean)
    {
        Goods goods = null;
        if (goodsBean != null)
        {
            goods = new Goods ();
            BeanUtils.copyProperties (goodsBean, goods, new String[]
            { "id" });
            goods.setId (StringUtils.isNotBlank (goodsBean.getId ()) ? Long.parseLong (goodsBean.getId ()) : 0);
            
            GoodsTypeBean goodsTypeBean = goodsBean.getTypeBean ();
            if (null != goodsTypeBean && StringUtils.isNotBlank (goodsTypeBean.getId ()))
            {
                GoodsType goodsType = new GoodsType (goodsTypeBean.getText ());
                goodsType.setId (StringUtils.isNotBlank (goodsTypeBean.getId ()) ? Long.parseLong (goodsTypeBean.getId ()) : 0);
                //goodsType.setParent (new GoodsType (Long.parseLong (goodsTypeBean.getParentId ())));
                goods.setType (goodsType);
            }
             
            /*if (StringUtils.isNotBlank (goodsBean.getGoodsTypeId ()))
            {
                GoodsType goodsType = new GoodsType (goodsBean.getGoodsTypeName ());
                goodsType.setId (Long.parseLong (goodsBean.getGoodsTypeId ()));
                goods.setType (goodsType);
            }*/
            GoodsUnitBean unitBean = goodsBean.getUnitBean ();
            if(null != unitBean && StringUtils.isNotBlank (unitBean.getId ())) 
            {
                goods.setUnit (goodsUnitBean2GoodsUnit (unitBean));
            }
            GoodsDepositoryBean depositoryBean = goodsBean.getDepositoryBean();
            if(null != depositoryBean && StringUtils.isNotBlank(depositoryBean.getId())) 
            {
                goods.setDepository(goodsDepositoryBean2GoodsDepository(depositoryBean));
            }
        }
        return goods;
    }

    public static GoodsBean goods2GoodsBean (Goods goods)
    {
        GoodsBean goodsBean = null;
        if (null != goods)
        {
            goodsBean = new GoodsBean ();
            goodsBean.setState ("open");
            BeanUtils.copyProperties (goods, goodsBean, new String[]
            { "id" });
            goodsBean.setId (goods.getId () + "");
            GoodsType goodsType = goods.getType ();
            if (null != goodsType)
            {
                GoodsTypeBean goodsTypeBean = new GoodsTypeBean ();
                goodsTypeBean.setId (goodsType.getId () + "");
                goodsTypeBean.setText (goodsType.getName ());
                //goodsTypeBean.setParentId (goodsType.getParent () != null ? goodsType.getParent ().getId () + "" : "");
                goodsBean.setTypeBean (goodsTypeBean);
                //goodsBean.setGoodsTypeId (goodsType.getId () + "");
                //goodsBean.setGoodsTypeName (goodsType.getName ());
            }
            GoodsStorage goodsStorage = goods.getStorage ();
            if (null != goodsStorage)
            {
                goodsBean.setStorageBean (storage2StorageBeanWithoutGoods (goodsStorage));
            }
            goodsBean.setUnitBean (goodsUnit2GoodsUnitBean (goods.getUnit ()));
            goodsBean.setDepositoryBean(goodsDepository2GoodsDepositoryBean(goods.getDepository()));
            goodsBean.setGoods (true);
        }
        return goodsBean;
    }

    public static List <GoodsBean> goodsList2GoodsBeanList (List <Goods> goodsList)
    {
        List <GoodsBean> goodsBeanList = new ArrayList <GoodsBean> ();
        if (null != goodsList)
        {
            for (Goods goods : goodsList)
            {
                goodsBeanList.add (goods2GoodsBean (goods));
            }
        }
        return goodsBeanList;
    }
    
    public static GoodsUnitBean goodsUnit2GoodsUnitBean (GoodsUnit goodsUnit) 
    {
        if(null != goodsUnit) 
        {
            GoodsUnitBean unitBean = new GoodsUnitBean ();
            unitBean.setId (goodsUnit.getId () + "");
            unitBean.setName (goodsUnit.getName ());
            return unitBean;
        }
        return null;
    }
    public static List<GoodsUnitBean> goodsUnitList2GoodsUnitBeanList (List<GoodsUnit> goodsUnits) 
    {
        List<GoodsUnitBean> unitBeans = new ArrayList <GoodsUnitBean> ();
        if(!CollectionUtils.isEmpty (goodsUnits)) 
        {
            for (GoodsUnit goodsUnit : goodsUnits) 
            {
                unitBeans.add (goodsUnit2GoodsUnitBean (goodsUnit));
            }
        }
        return unitBeans;
    } 
    public static GoodsUnit goodsUnitBean2GoodsUnit (GoodsUnitBean unitBean) 
    {
        if(null != unitBean) 
        {
            GoodsUnit unit = new GoodsUnit (unitBean.getName ());
            String unitId = unitBean.getId ();
            if (null != unitId && !"".equals (unitId))
            {
                unit.setId (Long.parseLong (unitId));
            }
            return unit;
        }
        return null;
    }

    public static CustomerBean customer2CustomerBean (Customer customer)
    {
        CustomerBean bean = null;
        if (null != customer)
        {
            bean = new CustomerBean ();
            BeanUtils.copyProperties (customer, bean, new String[]
            { "id" });
            bean.setId (customer.getId () + "");
            // bean.setType (customer.getTypeCode ().toString ());
            bean.setRegDate (DateUtils.date2String (customer.getRegisterDate (), DateUtils.DATE_PATTERN));
            bean.setRegFund (customer.getRegisterFund () + "");

            bean.setContactBeans (contactList2ContactBeanList (customer.getContacts ()));
            bean.setPaymentBeans (paymentList2PaymentBeanList (customer.getPayments ()));
            User user = DaoContext.getUserDao ().findOne (customer.getUserCreatedBy ());
            if(null != user) 
            {
                bean.setUserCreated (user.getUserId ());
            }
            user = DaoContext.getUserDao ().findOne (customer.getUserSigned ());
            if(null != user) 
            {
                bean.setUserSignedTo (user.getUserId ());
            }
        }
        return bean;
    }

    public static List <CustomerBean> customerList2CustomerBeanList (List <Customer> customers)
    {
        List <CustomerBean> customerBeans = new ArrayList <CustomerBean> ();
        if (!CollectionUtils.isEmpty (customers))
        {
            for (Customer customer : customers)
            {
                customerBeans.add (customer2CustomerBean (customer));
            }
        }
        return customerBeans;
    }

    public static Customer customerBean2Customer (CustomerBean bean)
    {
        Customer customer = null;
        if (null != bean)
        {
            customer = new Customer ();
            BeanUtils.copyProperties (bean, customer, new String[]
            { "id" });
            customer.setId (StringUtils.isNotBlank (bean.getId ()) ? Long.parseLong (bean.getId ()) : 0);
            // customer.setTypeCode (CustomerTypeCode.ENTERPRISE.equals
            // (bean.getType ()) ? CustomerTypeCode.ENTERPRISE
            // : CustomerTypeCode.INDIVIDUAL);
            customer.setRegisterDate (DateUtils.string2Date (bean.getRegDate (), DateUtils.DATE_PATTERN));
            if (!StringUtils.isBlank (bean.getRegFund ()))
            {
                customer.setRegisterFund(Long.parseLong(bean.getRegFund().lastIndexOf(".") > 0 ? bean.getRegFund().substring(
                    0,
                    bean.getRegFund().lastIndexOf(".")) : bean.getRegFund()));
            }
            customer.setPayments (paymentBeanList2PaymentList (bean.getPaymentBeans ()));
            List <Contact> contacts = contactBeanList2ContactList (bean.getContactBeans ());
            if (null != contacts)
            {
                customer.setContacts (contacts);
            }
            if(isNotBlank (bean.getUserCreated ())) 
            {
                customer.setUserCreatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (bean.getUserCreated ()));
            }
            if(isNotBlank (bean.getUserSignedTo ())) 
            {
                customer.setUserSigned (DaoContext.getUserDao ().getUniqueIdByUserId (bean.getUserSignedTo ()));
            }
        }
        return customer;
    }

    public static ContactBean contact2ContactBean (Contact contact)
    {
        ContactBean bean = null;
        if (null != contact)
        {
            bean = new ContactBean ();
            BeanUtils.copyProperties (contact, bean, new String[]
            { "id" });
            bean.setId (contact.getId () + "");
            Customer customer = contact.getCustomer ();
            if (null != customer)
            {
                bean.setCustomerId (customer.getId () + "");
                bean.setCustomerName (customer.getShortName ());
            }
            ContactType contactType = contact.getType ();
            if (null != contactType)
            {
                bean.setTypeBean (contactType2ContactTypeBean (contactType));
                //bean.setContactTypeId (contactType.getId () + "");
                //bean.setContactTypeName (contactType.getName ());
            }
            bean.setPhone (contact.getMobilePhone ());
            bean.setTecentQq (contact.getQq () + "");
            User user = DaoContext.getUserDao ().findOne (contact.getUserCreatedBy ());
            if(null != user) 
            {
                bean.setUserCreated (user.getUserId ());
            }
            user = DaoContext.getUserDao ().findOne (contact.getUserSigned ());
            if(null != user) 
            {
                bean.setUserSignedTo (user.getUserId ());
            }
        }
        return bean;
    }

    public static List <ContactBean> contactList2ContactBeanList (List <Contact> contacts)
    {
        List <ContactBean> contactBeans = new ArrayList <ContactBean> ();
        if (!CollectionUtils.isEmpty (contacts))
        {
            for (Contact contact : contacts)
            {
                contactBeans.add (contact2ContactBean (contact));
            }
        }
        return contactBeans;
    }

    public static Contact contactBean2Contact (ContactBean contactBean)
    {
        Contact contact = null;
        if (null != contactBean)
        {
            contact = new Contact ();
            BeanUtils.copyProperties (contactBean, contact, new String[]
            { "id" });
            contact.setId (StringUtils.isNotBlank (contactBean.getId ()) ? Long.parseLong (contactBean.getId ()) : 0);
            if (!StringUtils.isBlank (contactBean.getCustomerId ()))
            {
                Customer customer = new Customer ();
                customer.setId (Long.parseLong (contactBean.getCustomerId ()));
                customer.setShortName (contactBean.getCustomerName ());
                contact.setCustomer (customer);
            }
//            if (StringUtils.isNotBlank (contactBean.getContactTypeId ()))
//            {
//                ContactType contactType = new ContactType ();
//                contactType.setId (Long.parseLong (contactBean.getContactTypeId ()));
//                contact.setType (contactType);
//            }
            ContactTypeBean typeBean = contactBean.getTypeBean ();
            if (null != typeBean && StringUtils.isNotBlank (typeBean.getId ()))
            {
                contact.setType (contactTypeBean2ContactType (contactBean.getTypeBean ()));
            }
            if (!StringUtils.isBlank (contactBean.getPhone ()))
            {
                contact.setMobilePhone (contactBean.getPhone ());
            }
            if (!StringUtils.isBlank (contactBean.getTecentQq ()))
            {
                contact.setQq (Long.parseLong (contactBean.getTecentQq ()));
            }
            if(isNotBlank (contactBean.getUserCreated ())) 
            {
                contact.setUserCreatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (contactBean.getUserCreated ()));
            }
            if(isNotBlank (contactBean.getUserSignedTo ())) 
            {
                contact.setUserSigned (DaoContext.getUserDao ().getUniqueIdByUserId (contactBean.getUserSignedTo ()));
            }
        }
        return contact;
    }

    public static List <Contact> contactBeanList2ContactList (List <ContactBean> contactBeans)
    {
        List <Contact> contacts = null;
        if (!CollectionUtils.isEmpty (contactBeans))
        {
            contacts = new ArrayList <Contact> ();
            for (ContactBean contactBean : contactBeans)
            {
                contacts.add (contactBean2Contact (contactBean));
            }
        }
        return contacts;
    }

    public static ContactType contactTypeBean2ContactType (ContactTypeBean contactTypeBean)
    {
        ContactType contactType = null;
        if (null != contactTypeBean)
        {
            contactType = new ContactType (contactTypeBean.getName ());
            String unitId = contactTypeBean.getId ();
            if (null != unitId && !"".equals (unitId))
            {
                contactType.setId (Long.parseLong (unitId));
            }
        }
        return contactType;
    }

    public static ContactTypeBean contactType2ContactTypeBean (ContactType contactType)
    {
        ContactTypeBean contactTypeBean = null;
        if (null != contactType)
        {
            contactTypeBean = new ContactTypeBean ();
            contactTypeBean.setId (contactType.getId () + "");
            contactTypeBean.setName (contactType.getName ());
        }
        return contactTypeBean;
    }

    public static Order orderBean2Order (OrderBean orderBean)
    {
        Order order = null;
        if (null != orderBean)
        {
            order = new Order ();
            BeanUtils.copyProperties (orderBean, order, new String[]
            { "id" });
            if (StringUtils.isNotBlank (orderBean.getId ()))
            {
                order.setId (Long.parseLong (orderBean.getId ()));
            }
            order.setStatusCode (null == order.getStatusCode () || "".equals (order.getStatusCode ().toString ()) ? OrderStatusCode.NEW
                                                                                                                 : order.getStatusCode ());
            order.setOrderItems (orderItemBeanList2OrderItemList (orderBean.getOrderItemBeans ()));
            // if(!StringUtils.isBlank (orderBean.getCustomerId ()))
            // {
            // Customer customer = new Customer ();
            // customer.setId (Long.parseLong (orderBean.getCustomerId ()));
            // customer.setShortName (orderBean.getCustomerName ());
            // order.setCustomer (customer);
            // }
            if (null != orderBean.getCustomerBean () && StringUtils.isNotBlank (orderBean.getCustomerBean ().getId ()))
            {
                order.setCustomer (customerBean2Customer (orderBean.getCustomerBean ()));
            }
            if (null != orderBean.getContactBean () && StringUtils.isNotBlank (orderBean.getContactBean ().getId ()))
            {
                order.setContact (contactBean2Contact (orderBean.getContactBean ()));
            }
            if(isNotBlank (orderBean.getUserCreated ())) 
            {
                order.setUserCreatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (orderBean.getUserCreated ()));
            }
            if(isNotBlank (orderBean.getUserUpdated ())) 
            {
                order.setUserUpdatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (orderBean.getUserUpdated ()));
            }
//            if(isNotBlank (orderBean.getUserSignedTo ())) 
//            {
//                order.setUserSigned (DaoContext.getUserDao ().getUniqueIdByUserId (orderBean.getUserSignedTo ()));
//            }
            if(isNotBlank (orderBean.getUserOperated ())) 
            {
                order.setUserOperatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (orderBean.getUserOperated ()));
            }
        }
        return order;
    }

    public static OrderBean order2OrderBean (Order order)
    {
        OrderBean orderBean = null;
        if (null != order)
        {
            orderBean = new OrderBean ();
            BeanUtils.copyProperties (order, orderBean, new String[]
            { "id" });
            orderBean.setId (order.getId () + "");
            orderBean.setPayTime (DateUtils.date2String (order.getPayDate (), DateUtils.DATE_PATTERN));
            orderBean.setCreateTime (DateUtils.date2String (order.getCreateDate (), DateUtils.DATE_TIME_PATTERN));
            orderBean.setShipTime (DateUtils.date2String (order.getShipDate (), DateUtils.DATE_TIME_PATTERN));
            orderBean.setOrderItemBeans (orderItemList2OrderItemBeanList (order.getOrderItems ()));
            orderBean.setCustomerBean (customer2CustomerBean (order.getCustomer ()));
            orderBean.setContactBean (contact2ContactBean (order.getContact ()));
            
            User user = DaoContext.getUserDao ().findOne (order.getUserCreatedBy ());
            if(null != user) 
            {
                orderBean.setUserCreated (user.getUserId ());
            }
            user = DaoContext.getUserDao ().findOne (order.getUserUpdatedBy ());
            if(null != user) 
            {
                orderBean.setUserUpdated (user.getUserId ());
            }
//            user = DaoContext.getUserDao ().findOne (order.getUserSigned ());
//            if(null != user) 
//            {
//                orderBean.setUserSignedTo (user.getUserId ());
//            }
            user = DaoContext.getUserDao ().findOne (order.getUserOperatedBy ());
            if(null != user) 
            {
                orderBean.setUserOperated (user.getUserId ());
            }
            // if (null != order.getCustomer ())
            // {
            // orderBean.setCustomerId (order.getCustomer ().getId () + "");
            // orderBean.setCustomerName (order.getCustomer ().getShortName ());
            // }
            int payDays = order.getPayDays ();
            Calendar calendar = Calendar.getInstance ();
            calendar.setTimeInMillis (order.getCreateDate ().getTime ());
            calendar.set (Calendar.DATE, calendar.get (Calendar.DATE) + payDays);
            orderBean.setPayDeadline (DateUtils.date2String (calendar.getTime (), DateUtils.DATE_PATTERN));
        }
        return orderBean;
    }

    public static List <OrderBean> orderList2OrderBeanList (List <Order> orders)
    {
        List <OrderBean> orderBeans = new ArrayList <OrderBean> ();
        if (!CollectionUtils.isEmpty (orders))
        {
            for (Order order : orders)
            {
                orderBeans.add (order2OrderBean (order));
            }
        }
        return orderBeans;
    }

    public static OrderItemBean orderItem2OrderItemBean (OrderItem orderItem)
    {
        OrderItemBean orderItemBean = null;
        if (null != orderItem)
        {
            orderItemBean = new OrderItemBean ();
            BeanUtils.copyProperties (orderItem, orderItemBean, new String[]
            { "id" });
            orderItemBean.setId (orderItem.getId () + "");
            if (null != orderItem.getOrder ())
            {
                Order order = orderItem.getOrder ();
                orderItemBean.setOrderId (order.getId () + "");
                orderItemBean.setOrderCreate (DateUtils.date2String (order.getCreateDate (),
                                                                     DateUtils.DATE_TIME_PATTERN));
                if (null != order.getCustomer ())
                {
                    orderItemBean.setCustomerName (order.getCustomer ().getShortName ());
                }
            }
            if (null != orderItem.getGoods ())
            {
                orderItemBean.setGoodsId (orderItem.getGoods ().getId () + "");
                orderItemBean.setGoodsName (orderItem.getGoods ().getName ());
                orderItemBean.setGoodsUnit (orderItem.getGoods ().getUnit ().getName ());
            }
        }
        return orderItemBean;
    }

    public static OrderItem orderItemBean2OrderItem (OrderItemBean orderItemBean)
    {
        OrderItem orderItem = null;
        if (null != orderItemBean)
        {
            orderItem = new OrderItem ();
            BeanUtils.copyProperties (orderItemBean, orderItem, new String[]
            { "id" });
            orderItem.setId (StringUtils.isNotBlank (orderItemBean.getId ()) ? Long.parseLong (orderItemBean.getId ())
                                                                            : 0);
            if (!StringUtils.isBlank (orderItemBean.getGoodsId ()))
            {
                Goods goods = new Goods ();
                goods.setId (Long.parseLong (orderItemBean.getGoodsId ()));
                goods.setImportPrice (orderItemBean.getImportPrice ());
                orderItem.setGoods (goods);
            }
            if (!StringUtils.isBlank (orderItemBean.getOrderId ()))
            {
                Order order = new Order ();
                order.setId (Long.parseLong (orderItemBean.getOrderId ()));
                orderItem.setOrder (order);
            }
        }
        return orderItem;
    }

    public static List <OrderItemBean> orderItemList2OrderItemBeanList (List <OrderItem> orderItems)
    {
        List <OrderItemBean> orderItemBeans = new ArrayList <OrderItemBean> ();
        if (!CollectionUtils.isEmpty (orderItems))
        {
            for (OrderItem orderItem : orderItems)
            {
                orderItemBeans.add (orderItem2OrderItemBean (orderItem));
            }
        }
        return orderItemBeans;
    }

    public static List <OrderItem> orderItemBeanList2OrderItemList (List <OrderItemBean> orderItemBeans)
    {
        List <OrderItem> orderItems = new ArrayList <OrderItem> ();
        if (!CollectionUtils.isEmpty (orderItemBeans))
        {
            for (OrderItemBean orderItemBean : orderItemBeans)
            {
                orderItems.add (orderItemBean2OrderItem (orderItemBean));
            }
        }
        return orderItems;
    }

    public static PaymentBean payment2PaymentBean (Payment payment)
    {
        if (null != payment)
        {
            PaymentBean paymentBean = new PaymentBean ();
            BeanUtils.copyProperties (payment, paymentBean, new String[]
            { "id", "orderId", "projectId" });
            paymentBean.setId (payment.getId () + "");
            if (0 != payment.getOrderId ())
            {
                paymentBean.setOrderId (payment.getOrderId () + "");
            }
            if (0 != payment.getProjectId ())
            {
                paymentBean.setProjectId (payment.getProjectId () + "");
            }
            if (null != payment.getCustomer ())
            {
                paymentBean.setCustomerId (payment.getCustomer ().getId () + "");
                paymentBean.setCustomerName (payment.getCustomer ().getShortName ());
            }
            paymentBean.setPayDate (DateUtils.date2String (payment.getPaidDate (),
                                                           DateUtils.DATE_TIME_NO_SECOND_PATTERN));
            User user = DaoContext.getUserDao ().findOne (payment.getUserCreatedBy ());
            if(null != user) 
            {
                paymentBean.setOperator (user.getUserId ());
            }
            paymentBean.setAccountBean (paymentAccount2PaymentAccountBean (payment.getPaymentAccount ()));
            return paymentBean;
        }
        return null;
    }

    public static List <PaymentBean> paymentList2PaymentBeanList (List <Payment> payments)
    {
        if (!CollectionUtils.isEmpty (payments))
        {
            List <PaymentBean> paymentBeans = new ArrayList <PaymentBean> ();
            for (Payment payment : payments)
            {
                paymentBeans.add (payment2PaymentBean (payment));
            }
            return paymentBeans;
        }
        return null;
    }

    public static Payment paymentBean2Payment (PaymentBean paymentBean)
    {
        if (null != paymentBean)
        {
            Payment payment = new Payment ();
            BeanUtils.copyProperties (paymentBean, payment, new String[]
            { "id", "orderId", "projectId" });
            payment.setId (StringUtils.isNotBlank (paymentBean.getId ()) ? Long.parseLong (paymentBean.getId ()) : 0);
            payment.setPaidDate (DateUtils.string2Date (paymentBean.getPayDate () + " " + getCurrentHourAndMinute (),
                                                        DateUtils.DATE_TIME_NO_SECOND_PATTERN));
            if(isNotBlank (paymentBean.getOperator ())) 
            {
                payment.setUserCreatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (paymentBean.getOperator ()));
            }
            if (StringUtils.isNotBlank (paymentBean.getOrderId ()))
            {
                payment.setOrderId (Long.parseLong (paymentBean.getOrderId ()));
            }
            if (StringUtils.isNotBlank (paymentBean.getProjectId ()))
            {
                payment.setProjectId (Long.parseLong (paymentBean.getProjectId ()));
            }
            if (StringUtils.isNotBlank (paymentBean.getCustomerId ()))
            {
                Customer customer = new Customer ();
                customer.setId (Long.parseLong (paymentBean.getCustomerId ()));
                payment.setCustomer (customer);
            }
            payment.setPaymentAccount (paymentAccountBean2PaymentAccount (paymentBean.getAccountBean ()));
            return payment;
        }
        return null;
    }

    public static List <Payment> paymentBeanList2PaymentList (List <PaymentBean> paymentBeans)
    {
        if (!CollectionUtils.isEmpty (paymentBeans))
        {
            List <Payment> payments = new ArrayList <Payment> ();
            for (PaymentBean paymentBean : paymentBeans)
            {
                payments.add (paymentBean2Payment (paymentBean));
            }
            return payments;
        }
        return null;
    }

    public static PaymentAccount paymentAccountBean2PaymentAccount (PaymentAccountBean typeBean)
    {
        if (typeBean != null)
        {
            PaymentAccount type = new PaymentAccount ();
            type.setId (StringUtils.isNotBlank (typeBean.getId ()) ? Long.parseLong (typeBean.getId ()) : 0);
            type.setName (typeBean.getName ());
            type.setNumber (typeBean.getNumber ());
            return type;
        }
        return null;
    }

    public static PaymentAccountBean paymentAccount2PaymentAccountBean (PaymentAccount type)
    {
        if (type != null)
        {
            PaymentAccountBean typeBean = new PaymentAccountBean ();
            typeBean.setId (type.getId () + "");
            typeBean.setName (type.getName ());
            typeBean.setNumber (type.getNumber ());
            return typeBean;
        }
        return null;
    }

    public static GoodsStorageBean storage2StorageBean (GoodsStorage storage)
    {
        if (null != storage)
        {
            GoodsStorageBean storageBean = new GoodsStorageBean ();
            storageBean.setGoods (true);
            storageBean.setState ("open");
            BeanUtils.copyProperties (storage, storageBean, new String[]
            { "id", "goods" });
            storageBean.setId (storage.getId () + "");
            storageBean.setGoodsBean (goods2GoodsBean (storage.getGoods ()));
            storageBean.setCreateTime (DateUtils.date2String (storage.getCreateDate (), DateUtils.DATE_PATTERN));
            storageBean.setUpdateTime (DateUtils.date2String (storage.getLastUpdated (), DateUtils.DATE_PATTERN));
            User user = DaoContext.getUserDao ().findOne (storage.getUserCreatedBy ());
            if(null != user) 
            {
                storageBean.setUserCreated (user.getUserId ());
            }
            user = DaoContext.getUserDao ().findOne (storage.getUserUpdatedBy ());
            if(null != user) 
            {
                storageBean.setUserUpdated (user.getUserId ());
            }
            return storageBean;
        }
        return null;
    }

    public static GoodsStorageBean storage2StorageBeanWithoutGoods (GoodsStorage storage)
    {
        if (null != storage)
        {
            GoodsStorageBean storageBean = new GoodsStorageBean ();
            BeanUtils.copyProperties (storage, storageBean, new String[]
            { "id", "goods" });
            storageBean.setId (storage.getId () + "");
            storageBean.setCreateTime (DateUtils.date2String (storage.getCreateDate (), DateUtils.DATE_PATTERN));
            storageBean.setUpdateTime (DateUtils.date2String (storage.getLastUpdated (), DateUtils.DATE_PATTERN));
            User user = DaoContext.getUserDao ().findOne (storage.getUserCreatedBy ());
            if(null != user) 
            {
                storageBean.setUserCreated (user.getUserId ());
            }
            user = DaoContext.getUserDao ().findOne (storage.getUserUpdatedBy ());
            if(null != user) 
            {
                storageBean.setUserUpdated (user.getUserId ());
            }
            return storageBean;
        }
        return null;
    }

    public static List <GoodsStorageBean> storageList2StorageBeanList (List <GoodsStorage> storages)
    {
        List <GoodsStorageBean> storageBeans = new ArrayList <GoodsStorageBean> ();
        if (!CollectionUtils.isEmpty (storages))
        {
            for (GoodsStorage storage : storages)
            {
                storageBeans.add (storage2StorageBean (storage));
            }
        }
        return storageBeans;
    }

    public static GoodsStorage storageBean2Storage (GoodsStorageBean storageBean)
    {
        if (null != storageBean)
        {
            GoodsStorage storage = new GoodsStorage ();
            BeanUtils.copyProperties (storageBean, storage, new String[]
            { "id" });
            storage.setId (StringUtils.isNotBlank (storageBean.getId ()) ? Long.parseLong (storageBean.getId ()) : 0);
            storage.setGoods (goodsBean2Goods (storageBean.getGoodsBean ()));
            storage.setCreateDate (DateUtils.string2Date (storageBean.getCreateTime (), DateUtils.DATE_PATTERN));
            storage.setLastUpdated (DateUtils.string2Date (storageBean.getUpdateTime (), DateUtils.DATE_PATTERN));
            if(isNotBlank (storageBean.getUserCreated ())) 
            {
                storage.setUserCreatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (storageBean.getUserCreated ()));
            }
            if (isNotBlank (storageBean.getUserUpdated ())) 
            {
                storage.setUserUpdatedBy (DaoContext.getUserDao ().getUniqueIdByUserId (storageBean.getUserUpdated ()));
            }
            return storage;
        }
        return null;
    }

    public static AccountingType accountingTypeBean2AccountingType (AccountingTypeBean typeBean)
    {
        if (null != typeBean)
        {
            AccountingType type = new AccountingType ();
            type.setId (StringUtils.isNotBlank (typeBean.getId ()) ? Long.parseLong (typeBean.getId ()) : 0);
            type.setName (typeBean.getName ());
            type.setAccountingMode(typeBean.getAccountingMode());
            type.setDescription (typeBean.getDescription ());
            return type;
        }
        return null;
    }

    public static AccountingTypeBean accountingType2AccountingTypeBean (AccountingType type)
    {
        if (null != type)
        {
            AccountingTypeBean typeBean = new AccountingTypeBean ();
            typeBean.setId (type.getId () + "");
            typeBean.setName (type.getName ());
            typeBean.setAccountingMode(type.getAccountingMode());
            typeBean.setDescription (type.getDescription ());
            return typeBean;
        }
        return null;
    }

    public static List <AccountingTypeBean> accountingTypeList2AccountingTypeBeanList (List <AccountingType> types)
    {
        List <AccountingTypeBean> typeBeans = new ArrayList <AccountingTypeBean> ();
        if (!CollectionUtils.isEmpty (types))
        {
            for (AccountingType type : types)
            {
                typeBeans.add (accountingType2AccountingTypeBean (type));
            }
        }
        return typeBeans;
    }
    
    public static Accounting accountingBean2Accounting (AccountingBean accountingBean) 
    {
        if(null != accountingBean) 
        {
            Accounting accounting = new Accounting ();
            BeanUtils.copyProperties (accountingBean, accounting, new String[]{"id"});
            accounting.setId(isNotBlank(accountingBean.getId()) ? Long.parseLong(accountingBean.getId()) : 0);
            accounting.setType (accountingTypeBean2AccountingType (accountingBean.getTypeBean ()));
            if (isNotBlank(accountingBean.getCreateDate()))
            {
                accounting.setCreated(DateUtils.string2Date(
                    accountingBean.getCreateDate(),
                    DateUtils.DATE_TIME_NO_SECOND_PATTERN));
            }
            if (isNotBlank(accountingBean.getUpdateDate()))
            {
                accounting.setUpdated(DateUtils.string2Date(
                    accountingBean.getUpdateDate(),
                    DateUtils.DATE_TIME_NO_SECOND_PATTERN));
            }
            if (isNotBlank(accountingBean.getUserCreated()))
            {
                accounting.setUserCreatedBy(DaoContext.getUserDao().getUniqueIdByUserId(accountingBean.getUserCreated()));
            }
            if (isNotBlank(accountingBean.getUserUpdated()))
            {
                accounting.setUserUpdatedBy(DaoContext.getUserDao().getUniqueIdByUserId(accountingBean.getUserUpdated()));
            }
            return accounting;
        }
        return null;
    }
    
    public static AccountingBean accounting2AccountingBean (Accounting accounting) 
    {
        if(null != accounting) 
        {
            AccountingBean accountingBean = new AccountingBean ();
            BeanUtils.copyProperties (accounting, accountingBean, new String[]{"id"});
            accountingBean.setId(accounting.getId() + "");
            accountingBean.setTypeBean (accountingType2AccountingTypeBean (accounting.getType ()));
            accountingBean.setCreateDate (DateUtils.date2String (accounting.getCreated (), DateUtils.DATE_TIME_NO_SECOND_PATTERN));
            accountingBean.setUpdateDate (DateUtils.date2String (accounting.getUpdated (), DateUtils.DATE_TIME_NO_SECOND_PATTERN));
            User user = DaoContext.getUserDao ().findOne (accounting.getUserCreatedBy ());
            if(null != user) 
            {
                accountingBean.setUserCreated (user.getUserId ());
            }
            user = DaoContext.getUserDao ().findOne (accounting.getUserUpdatedBy ());
            if(null != user) 
            {
                accountingBean.setUserUpdated (user.getUserId ());
            }
            
            return accountingBean;
        }
        return null;
    }
    
    public static List<AccountingBean> accountingList2AccountingBeanList (List<Accounting> accountings) 
    {
        if(null != accountings) 
        {
            List<AccountingBean> accountingBeans = new ArrayList<AccountingBean> ();
            for(Accounting accounting : accountings) 
            {
                accountingBeans.add (accounting2AccountingBean(accounting));
            }
            return accountingBeans;
        }
        return null;
    }

    public static OrderSearchForm searchRequestForm2OrderSearchForm (SearchRequestForm searchRequestForm)
    {
        if (null != searchRequestForm)
        {
            OrderSearchForm orderSearchForm = new OrderSearchForm ();
            if (StringUtils.isNotBlank (searchRequestForm.getStartDate ())
                && StringUtils.isNotBlank (searchRequestForm.getEndDate ()))
            {
                orderSearchForm.setStartDate (DateUtils.string2Date (searchRequestForm.getStartDate () + " 00:00:00",
                                                                     DateUtils.DATE_TIME_PATTERN));
                orderSearchForm.setEndDate (DateUtils.string2Date (searchRequestForm.getEndDate () + " 23:59:59",
                                                                   DateUtils.DATE_TIME_PATTERN));
            }
            orderSearchForm.setCustomerId(StringUtils.isNotBlank(searchRequestForm.getCustomerId()) ? Long.parseLong(searchRequestForm
                .getCustomerId()) : 0);
            orderSearchForm.setOrderId(StringUtils.isNotBlank(searchRequestForm.getOrderId()) ? Long.parseLong(searchRequestForm
                .getOrderId()) : 0);
            return orderSearchForm;
        }
        return null;
    }

    public static PaymentSearchForm searchRequestForm2PaymentSearchForm (SearchRequestForm searchRequestForm)
    {
        if (null != searchRequestForm)
        {
            PaymentSearchForm paymentSearchForm = new PaymentSearchForm ();
            if (StringUtils.isNotBlank (searchRequestForm.getStartDate ())
                && StringUtils.isNotBlank (searchRequestForm.getEndDate ()))
            {
                paymentSearchForm.setStartDate (DateUtils.string2Date (searchRequestForm.getStartDate () + " 00:00:00",
                                                                       DateUtils.DATE_TIME_PATTERN));
                paymentSearchForm.setEndDate (DateUtils.string2Date (searchRequestForm.getEndDate () + " 23:59:59",
                                                                     DateUtils.DATE_TIME_PATTERN));
            }
            paymentSearchForm.setCustomerId (StringUtils.isNotBlank (searchRequestForm.getCustomerId ()) ? Long.parseLong (searchRequestForm.getCustomerId ())
                                                                                                        : 0);
            paymentSearchForm.setPaymentId (StringUtils.isNotBlank (searchRequestForm.getPaymentId ()) ? Long.parseLong (searchRequestForm.getPaymentId ())
                                                                                                      : 0);
            return paymentSearchForm;
        }
        return null;
    }

    public static AccountingSearchForm searchRequestForm2AccountingSearchForm (SearchRequestForm searchRequestForm)
    {
        if (null != searchRequestForm)
        {
            AccountingSearchForm accountingSearchForm = new AccountingSearchForm ();
            if (StringUtils.isNotBlank (searchRequestForm.getStartDate ())
                && StringUtils.isNotBlank (searchRequestForm.getEndDate ()))
            {
                accountingSearchForm.setStartDate (DateUtils.string2Date (searchRequestForm.getStartDate () + " 00:00:00",
                                                                       DateUtils.DATE_TIME_PATTERN));
                accountingSearchForm.setEndDate (DateUtils.string2Date (searchRequestForm.getEndDate () + " 23:59:59",
                                                                     DateUtils.DATE_TIME_PATTERN));
            }
            accountingSearchForm.setAccountingTypeId(StringUtils.isNotBlank(searchRequestForm.getAccountingTypeId()) ? Long
                .parseLong(searchRequestForm.getAccountingTypeId()) : 0);
            accountingSearchForm
                .setAccountingModeCode(convertAccountingModeCode (searchRequestForm.getAccountingMode ()));
            return accountingSearchForm;
        }
        return null;
    }
    
    private static AccountingModeCode convertAccountingModeCode (String mode) 
    {
        AccountingModeCode modeCode = null;
        if(isNotBlank (mode)) 
        {
            if(AccountingModeCode.IN_COME.toString().equalsIgnoreCase (mode)) 
            {
                modeCode = AccountingModeCode.IN_COME;
            }
            else if(AccountingModeCode.OUT_LAY.toString().equalsIgnoreCase (mode)) 
            {
                modeCode = AccountingModeCode.OUT_LAY;
            }
        }
        return modeCode;
    }

    public static LogSearchForm logSearchRequestForm2LogSearchForm (LogSearchRequestForm searchRequestForm)
    {
        if (null != searchRequestForm)
        {
            LogSearchForm logSearchForm = new LogSearchForm ();
            if (StringUtils.isNotBlank (searchRequestForm.getStartDate ())
                && StringUtils.isNotBlank (searchRequestForm.getEndDate ()))
            {
                logSearchForm.setStartDate (DateUtils.string2Date (searchRequestForm.getStartDate () + " 00:00:00",
                                                                   DateUtils.DATE_TIME_PATTERN));
                logSearchForm.setEndDate (DateUtils.string2Date (searchRequestForm.getEndDate () + " 23:59:59",
                                                                 DateUtils.DATE_TIME_PATTERN));
            }
            logSearchForm.setUserId (searchRequestForm.getUserId ());
            logSearchForm.setLogType (searchRequestForm.getLogType ());
            return logSearchForm;
        }
        return null;
    }

    private static String getCurrentHourAndMinute ()
    {
        Calendar c = Calendar.getInstance ();
        c.setTime (new Date ());
        return c.get (Calendar.HOUR_OF_DAY) + ":" + c.get (Calendar.MINUTE);
    }

    public static GoodsStorageCourseBean goodsStorageCourse2GoodsStorageCourseBean (GoodsStorageCourse goodsStorageCourse)
    {
        if (null != goodsStorageCourse)
        {
            GoodsStorageCourseBean goodsStorageCourseBean = new GoodsStorageCourseBean ();
            BeanUtils.copyProperties (goodsStorageCourse, goodsStorageCourseBean);
            goodsStorageCourseBean.setOrderCreateDate (DateUtils.date2String (goodsStorageCourse.getOrderCreate (),
                                                                              DateUtils.DATE_TIME_PATTERN));
            return goodsStorageCourseBean;
        }
        return null;
    }
    
    public static GoodsDepository goodsDepositoryBean2GoodsDepository (GoodsDepositoryBean bean) 
    {
        if(null != bean) 
        {
            GoodsDepository goodsDepository = new GoodsDepository ();
            goodsDepository.setId(isNotBlank(bean.getId()) ? Long.parseLong(bean.getId()) : 0);
            goodsDepository.setName(bean.getName());
            goodsDepository.setLocation(bean.getLocation());
            goodsDepository.setOtherInfo(bean.getOtherInfo());
            return goodsDepository;
        }
        return null;
    }
    
    public static GoodsDepositoryBean goodsDepository2GoodsDepositoryBean (GoodsDepository depository) 
    {
        if(null != depository) 
        {
            GoodsDepositoryBean bean = new GoodsDepositoryBean ();
            bean.setId(depository.getId() + "");
            bean.setName(depository.getName());
            bean.setLocation(depository.getLocation());
            bean.setOtherInfo(depository.getOtherInfo());
            return bean;
        }
        return null;
    }
    
    public static boolean isNotBlank (String s) 
    {
        return StringUtils.isNotBlank (s) && !"null".equals (s.trim ());
    }
}
